/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import Params.ChatMessage;
import Params.ServerSocketItem;

/**
 *
 * @author Administrator
 */
public class frmCMD extends javax.swing.JInternalFrame implements ActionListener {

    Socket _sockdangchon = null;

    /**
     * Creates new form frmCMD
     */
    public frmCMD() {
        initComponents();
        this.toFront();
        this.txtshell.addActionListener(this);
    }

    frmCMD(Socket temp) {
        initComponents();
        this.toFront();
        this.txtshell.addActionListener(this);
        //initServer(strPort);
        _sockdangchon = temp;
        int temp1 = (temp.getPort());
        this.title = "SHELL CMD - CLIENT " + temp1 + "";
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        for (int i = 0; i < MainSever.listConnection.size(); i++) {
            if (!this.txtshell.getText().equals("")) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                if (temp.getConnectionshell().connectionSocket != null) {
                    if (temp.getSOCKM().getPort() == _sockdangchon.getPort() + 4) {
                        temp.getConnectionshell().bufferedWriter.println(this.txtshell.getText().toString());
                        temp.getConnectionshell().bufferedWriter.flush();
                    }

                }
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtshell = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jthongbao = new javax.swing.JTextArea();

        setClosable(true);
        setTitle("CMD");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Câu Lệnh Shell :");

        jButton1.setText("CMD");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jthongbao.setColumns(20);
        jthongbao.setLineWrap(true);
        jthongbao.setRows(5);
        jthongbao.setText("Nội Dung Thông Bào");
        jScrollPane1.setViewportView(jthongbao);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(0, 179, Short.MAX_VALUE)
                .add(jButton1)
                .add(177, 177, 177))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(txtshell)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(jScrollPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 17, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(txtshell, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 50, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 66, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        for (int i = 0; i < MainSever.listConnection.size(); i++) {
            if (!this.txtshell.getText().equals("")) {
                ServerSocketItem temp = (ServerSocketItem) MainSever.listConnection.elementAt(i);
                if (temp.getConnectionshell().connectionSocket != null) {
                    if (temp.getSockshell().getPort() == (_sockdangchon.getPort()) + 4) {
                        temp.getConnectionshell().bufferedWriter.println(this.txtshell.getText().toString());
                        temp.getConnectionshell().bufferedWriter.flush();
                    }
                }
            }
        }
        
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jthongbao;
    private javax.swing.JTextField txtshell;
    // End of variables declaration//GEN-END:variables

    void onRead(ChatMessage message) {
        jthongbao.setText(message.getMessage());
    }
}
